import time
import sys
import json
import io
# 设置sys.stdout的编码为UTF-8
sys.stdout.reconfigure(encoding='utf-8')

from DrissionPage._pages.chromium_tab import ChromiumTab

# 页面中点击事件 和输入框
def page_click_input(tab: ChromiumTab,paramBeans):
    if not paramBeans:
        return

    for paramBean in paramBeans:
        actionType = paramBean['actionType']
        elementPath = paramBean['elementPath']
        paramValue = None
        if 'paramValue' in paramBean:
            paramValue = paramBean['paramValue']

        page_element = tab.ele('xpath:' + elementPath)
        if not page_element:
            continue
        tab.scroll.to_see(page_element)

        if 'click' == actionType:
            page_element.click()
            time.sleep(1)

        if 'input' == actionType and paramValue:
            page_element.click()
            time.sleep(1)
            page_element.clear()
            time.sleep(1)
            page_element.input(paramValue)
            time.sleep(1)

        if 'search' == actionType and paramValue:
            # 点击按钮,弹出框
            page_element.click()
            time.sleep(1)
            # 弹出框
            search_top_div = tab.ele('tag:div@@class=next-overlay-wrapper opened')
            # 搜索框
            search_input = search_top_div.ele('tag:input')
            search_input.click()
            time.sleep(1)
            search_input.input(paramValue)
            time.sleep(1)
            # 结果
            search_result = search_top_div.ele('tag:div@@class=info-content@@text()=' + paramValue)
            if search_result :
                search_result.click()
                time.sleep(1)



def color_chima_beans(tab: ChromiumTab,paramBeans):
    index = 0
    for colorBean in paramBeans:
        colorTopDivId = colorBean['colorTopDivId']
        colorName = colorBean['colorName']

        colorInputPlaceholder = colorBean['colorInputPlaceholder']
        descInputPlaceholder = colorBean['descInputPlaceholder']
        addText = colorBean['addText']
        addColor = colorBean['addColor']

        color_top_div = tab.ele('#' + colorTopDivId)
        color_top_div.click()
        # 颜色文本: 只有一个没有填写内容的input
        color_inputs = color_top_div.eles('tag:input@@placeholder^' + colorInputPlaceholder)
        color_inputs[index].click()
        time.sleep(1)
        color_inputs[index].input(colorName)
        time.sleep(1)

        # 颜色备注
        if 'colorDesc' in colorBean:
            colorDesc = colorBean['colorDesc']
            desc_inputs = color_top_div.eles('tag:input@@placeholder^' + descInputPlaceholder)
            desc_inputs[index].click()
            time.sleep(1)
            desc_inputs[index].input(colorDesc)
            time.sleep(1)
        # 搜索图片
        if 'searchTypeBean' in colorBean:
            searchTypeBean = colorBean['searchTypeBean']
            if searchTypeBean:
                selectImageClass = colorBean['selectImageClass']
                # 选择图片
                add_elements = color_top_div.eles('tag:div@@class=' + selectImageClass)
                add_elements[index].click()
                time.sleep(2)
                from . import tmall_iframe_search_image_util
                tmall_iframe_search_image_util.frame_search_image(tab, searchTypeBean)

        index = index + 1

        # 新增按钮
        if addColor:
            select_image_ele = color_top_div.ele('tag:span@@text():' + addText)
            select_image_ele.click()
            time.sleep(1)




